<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 演示示例
//-------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use mailer\tp5\Mailer;

class Demo extends Controller
{
    /**
     * Excel一键导出
     */
    public function excel()
    {
        return $this->fetch();
    }
    public function addcolumn()
    {
        $id = input('id');
        if($id ==null){
            $id =0;
        }
        $this->assign('id', $id);
        // var_dump($id);die;
        return $this->fetch();
    }
    public function column(){

        $param = $this->request->param();
        
        $id = (array)(json_decode(array_flip($param)[""]));//先将键值对互换，json解析，在取值;
        
        // echo "<br />";
        $where = [
            'status' => 1,
            'is_delete' => 0,
        ];
        if(!empty($id["id"]) || isset($id["id"])){
            $where['parent_id'] = $id["id"];
            $field = 'id, name, parent_id as parentId, 0 as isParent';
        } else {
            $where['parent_id'] = 0;
            $field = 'id, name, parent_id as parentId, 1 as isParent';
        }
        // var_dump($where); 
        
        $firstMenu = Db::name('typefile')->where($where)->field($field)->select();
        // print_r($firstMenu);
        // die;
        return json($firstMenu);

        // $this->assign('firstMenu', $firstMenu);

    }

    /**
     * 修改菜单信息
     * @return array
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function updateMenu () {
        if ($this->request->isPost()) {
            if ($this->request->isPost()) {
                $param = $this->request->param();
                $where = [
                    'id' => $param['id'],
                    'is_delete' => 0,
                ];

                $data = [
                    'name' => $param['name']
                ];
                $res = Db::name('typefile')->where($where)->update($data);

                if ($res) {
                    return [
                        'msg' => '成功',
                        'success' => true,
                        'data' => [],
                    ];
                } else {
                    return [
                        'msg' => '失败',
                        'success' => false,
                        'data' => [],
                    ];
                }
            }
        }
    }

    /**
     * 添加菜单项
     * @return array
     */
    public function addMenu () {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $data = [
                'name' => $param['name'],
                'parent_id' => $param['parent_id'],
                'status' => 1,
            ];

            $res = Db::name('typefile')->insert($data);

            if ($res) {
                return [
                    'msg' => '成功',
                    'success' => true,
                    'data' => [],
                ];
            } else {
                return [
                    'msg' => '失败',
                    'success' => false,
                    'data' => [],
                ];
            }
        }
    }

    /**
     * 删除菜单项
     * @return array
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function deleteMenu () {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $where = [
                'id' => $param['id'],
                'is_delete' => 0,
            ];
          
            $data = [
                'is_delete' => 1
            ];
            $res = Db::name('typefile')->where($where)->update($data);

            if ($res) {
                return [
                    'msg' => '成功',
                    'success' => true,
                    'data' => [],
                ];
            } else {
                return [
                    'msg' => '失败',
                    'success' => false,
                    'data' => [],
                ];
            }
        }
    }
    /**
     * 下载文件
     * @return mixed
     */
    public function download()
    {
        if ($this->request->param('file')) {
            return \File::download("../README.md");
        } else {
            return $this->view->fetch();
        }
    }

    /**
     * 下载远程图片
     * @return mixed
     */
    public function downloadImage()
    {
        if (Request::instance()->isPost()) {
            $url = $this->request->post("url");
            if (substr($url, 0, 4) != "http") {
                return ajax_return_adv_error("url非法");
            }
            $name = "./tmp/" . get_random();
            $filename = \File::downloadImage($url, $name);
            if (!$filename) {
                return ajax_return_adv_error($filename);
            } else {
                $url = $this->request->root() . substr($filename, 1);

                return ajax_return_adv("下载成功", '', "图片下载成功，<a href='{$url}' target='_blank' class='c-blue'>点击查看</a><br>{$url}");
            }
        } else {
            return $this->view->fetch();
        }
    }

    /**
     * 发送邮件
     * @return mixed
     */
    public function mail()
    {
        if ($this->request->isPost()) {
            $receive = $this->request->post("receiver");
            $result = $this->validate(
                ['receiver' => $receive],
                ['receiver|收件人' => 'require|email']
            );
            if ($result !== true) {
                return ajax_return_adv_error($result);
            }
            $mailer = Mailer::instance();
            $result = $mailer->to($receive)
                ->subject('来自tpadmin的测试邮件')
                ->view('mail_template')
                ->send();

            if ($result == 0) {
                return ajax_return_adv_error($mailer->getError());
            } else {
                return ajax_return_adv("邮件发送成功，请注意查收", '');
            }
        } else {
            return $this->view->fetch();
        }
    }

    /**
     * 百度编辑器
     * @return mixed
     */
    public function ueditor()
    {
        return $this->view->fetch();
    }

    /**
     * 七牛上传
     * @return mixed
     */
    public function qiniu()
    {
        if ($this->request->isPost()) {
//            return '<script>parent.layer.alert("仅做演示")</script>';
            /*$result = \Qiniu::instance()->upload();
            p($result);*/

        } else {
            $map = [
                'f.is_delete' => 0
            ];
            $data = Db::name('files f')
                ->where($map)
                ->join('typefile tf', 'f.type = tf.id', 'left')
                ->join('typefile tf1', 'f.stype = tf1.id', 'left')
                ->field('f.*, tf.name as type_name, tf1.name as stype_name')
                ->order('f.sort desc, f.create desc')
                ->select();

            $this->assign('list', $data);

//            $list = Db::name('files')->where(array('is_delete' => 0))->field('id,stype')->select();
//            foreach ($list as $k => &$v) {
//                Db::name('files') ->where(array('id'=>$v['id'], 'is_delete' => 0))->update(array('sort' => $v['stype']));
//            }
        }
        return $this->view->fetch();
    }

    /**
     * ID加密
     * @return mixed
     */
    public function hashids()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post("id");
            $hashids = \Hashids\Hashids::instance(8, "tpadmin");
            $encode_id = $hashids->encode($id); //加密
            $decode_id = $hashids->decode($encode_id); //解密
            return ajax_return_adv("操作成功", '', false, '', '', ['encode' => $encode_id, 'decode' => $decode_id]);
        } else {
            return $this->view->fetch();
        }
    }

    /**
     * 丰富弹层
     */
    public function layer()
    {
        return $this->view->fetch();
    }

    /**
     * 表格溢出
     */
    public function tableFixed()
    {
        return $this->view->fetch();
    }

    /**
     * 图片上传回调
     */
    public function imageUpload()
    {

        //
        $where = [
            'status' => 1,
            'parent_id' => 0,
        ];
        $firstMenu = Db::name('typefile')->where($where)->field('id, name')->select();

        $this->assign('firstMenu', $firstMenu);
        return $this->view->fetch();
    }

    public function imageUploadedit()
    {
        if ($this->request->isGet()) {
            $param = $this->request->param();
            $map = [
                'f.id' => $param['id'],
            ];
            $data = Db::name('files f')
                ->where($map)
                ->join('typefile tf', 'f.type = tf.id', 'left')
                ->join('typefile tf1', 'f.stype = tf1.id', 'left')
                ->field('f.*, tf.name as type_name, tf1.name as stype_name')
                ->order('f.create desc')
                ->find();
            $this->assign('list', $data);
            //一级导航
            $where = [
                'status' => 1,
                'parent_id' => 0,
            ];
            $firstMenu = Db::name('typefile')->where($where)->field('id, name')->select();
    
            $this->assign('firstMenu', $firstMenu);

            //二级导航
            $where = [
                'status' => 1,
                'parent_id' => $data['type'],
            ];
            $secondMenu = Db::name('typefile')->where($where)->field('id, name')->select();
    
            $this->assign('secondMenu', $secondMenu);

        }
        return $this->fetch();
    }

    public function deleteData () {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            // echo $param['id'];
            $where = [
                'id' => $param['id'],
            ];
            $data = [
                'is_delete' => 1
            ];
            $res = Db::name('files')->where($where)->update($data);
            // var_dump($res);
            // die;
            if ($res) {
                return [
                    'success' => true,
                    'msg' => '成功',
                    'data' => [],
                ];
            } else {
                return [
                    'success' => false,
                    'msg' => '失败',
                    'data' => []
                ];
            }
        }
    }

    /**
     * 二维码生成
     */
    public function qrcode()
    {
        return $this->view->fetch();
    }
}